<%@page import="com.dilean.api.dao.AttributeListDAO"%>
<%@page import="com.dilean.api.model.AttributeList"%>
<%@page import="java.util.HashSet"%>
<%@page import="com.dilean.api.model.AttributeListValue"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%-- imports --%>
<%@page import="com.dilean.api.dao.AttributeTypeDAO"%>
<%@page import="com.dilean.impl.model.AttributeTypeImpl"%>
<%@page import="com.dilean.api.model.AttributeType"%>
<%@page import="com.dilean.spring.AplicationContextHolder"%>
<%@page import="com.dilean.api.dao.PersonDAO"%>
<%@page import="com.dilean.impl.model.PersonImpl"%>
<%@page import="com.dilean.api.model.Person"%>
<%@page import="com.dilean.impl.model.AttributeListValueImpl"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueDate"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueList"%>
<%@page import="java.math.BigInteger"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueTextMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueNumber"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueNumberMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>
<%@page import="java.util.Collection"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.dilean.spring.AplicationContextHolder"%>
<%@page import="com.dilean.api.dao.PersonDAO"%>
<%@page import="com.dilean.impl.model.PersonImpl"%>
<%@page import="com.dilean.api.model.Person"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueListMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueList"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueDate"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueNumberMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueTextMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>

<!DOCTYPE html>
<html lang="en">
    <head>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">

        <!-- CSS and other resourses -->
        <link href="resources/mytheme/css/bootstrap.min.css" rel="stylesheet">

        <!-- Custom CSS -->
        <link href="resources/mytheme/css/simple-sidebar.css" rel="stylesheet">


        <link href="resources/mytheme/css/font-awesome.css" rel="stylesheet">

        <link href="resources/mytheme/css/font-awesome.min.css" rel="stylesheet">


        <%
            int myId = Integer.parseInt(request.getParameter("id"));

            Person me = new PersonImpl();

            me = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findByIdWithFriends(myId);

            AttrValueText firstName = me.getAttributeValue(1, AttrValueText.class);
            AttrValueText lastName = me.getAttributeValue(2, AttrValueText.class);
            AttrValueTextMultiple emails = me.getAttributeValue(3, AttrValueTextMultiple.class);
            AttrValueNumberMultiple telephone = me.getAttributeValue(4, AttrValueNumberMultiple.class);
            AttrValueDate birthDate = me.getAttributeValue(10, AttrValueDate.class);
            AttrValueText status = me.getAttributeValue(5, AttrValueText.class);
            AttrValueDate regDate = me.getAttributeValue(11, AttrValueDate.class);
            AttrValueList gender = me.getAttributeValue(6, AttrValueList.class);
            AttrValueList relStatus = me.getAttributeValue(7, AttrValueList.class);
            AttrValueList univercity = me.getAttributeValue(9, AttrValueList.class);
            AttrValueListMultiple languages = me.getAttributeValue(8, AttrValueListMultiple.class);

            List<AttributeListValue> lanValues = languages.getValue();

            int lanId = 3;
            int genId = 1;
            int relId = 2;
            int unId = 4;

            AttributeList attrList = AplicationContextHolder.getInstance().getContext().getBean(AttributeListDAO.class).findByIdWithListValues(lanId);
            Collection<AttributeListValue> list = attrList.getAttributeListValues();

            AttributeList attrListGen = AplicationContextHolder.getInstance().getContext().getBean(AttributeListDAO.class).findByIdWithListValues(genId);
            Collection<AttributeListValue> genList = attrListGen.getAttributeListValues();
            
            AttributeList attrListRel = AplicationContextHolder.getInstance().getContext().getBean(AttributeListDAO.class).findByIdWithListValues(relId);
            Collection<AttributeListValue> relList = attrListRel.getAttributeListValues();
            
            AttributeList attrListUn = AplicationContextHolder.getInstance().getContext().getBean(AttributeListDAO.class).findByIdWithListValues(unId);
            Collection<AttributeListValue> unList = attrListUn.getAttributeListValues();

            Collection<Person> friendRequests = me.getFriendRequests();
            Collection<Person> friends = me.getFriends();
            int numberRequest = 0;
            for (Person fr : friendRequests) {
                if (friends.contains(fr) == false) {
                    numberRequest++;
                }
            }

            String fullname = "User";
            if ((firstName != null) && (lastName != null)) {
                fullname = firstName.getValue() + " " + lastName.getValue();
            } else if ((firstName == null) && (lastName != null)) {
                fullname = lastName.getValue();
            } else if ((firstName != null) && (lastName == null)) {
                fullname = firstName.getValue();
            }


        %>     
        <title>Edit my Page &middot; Dilean</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
        <style>
            #tab_logic{
                width:100%;
            }
            #tab_logic1{
                width:100%;
            }
        </style>
    </head>
    <body>
        <div id="wrapper">

            <!-- Sidebar -->
            <div id="sidebar-wrapper">
                <<ul class="sidebar-nav">
                    <li class="sidebar-brand">
                        <a href="http://localhost:8080/Dilean/myPage.jsp">
                            <h2 align="centre" >Dilean</h2>
                        </a>
                    </li>
                    <li>
                        <a href="http://localhost:8080/Dilean/myPage.jsp"><i class="fa fa-home fa-fw-5x"></i>&nbsp; My Page</a>
                    </li>
                    <li>
                        <a href="http://localhost:8080/Dilean/friends.jsp?id=1"><i class="fa fa-users"></i>&nbsp; Friends </a>
                    </li>

                    <li>
                        <a href="http://localhost:8080/Dilean/allUsers.jsp?id=1"><i class="fa fa-database"></i>&nbsp; Users 
                            <%                                if (numberRequest != 0) {
                            %>
                            <span class="badge badge-info"><%=numberRequest%> </span>
                            <%
                                }
                            %>
                        </a>
                    </li>



                    <li>
                        <a href="http://localhost:8080/Dilean/conversation.jsp"><i class="fa fa-envelope"></i>&nbsp; Messages </a>
                    </li>
                    <li class="divider"></li>
                    <li>
                        <a href="http://localhost:8080/Dilean/editUser.jsp?id=1"><i class="fa fa-cog"></i>&nbsp; Settings</a>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-book"></i>&nbsp; About</a>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-sign-out"></i>&nbsp; LogOut</a>
                    </li>

                </ul>
            </div>
            <!-- /#sidebar-wrapper -->

            <!-- Page Content -->
            <div id="page-content-wrapper">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-lg-12">


                            <a class="btn btn-large btn-primary" href="#menu-toggle" id="menu-toggle" >


                                <i class="fa fa-user fa-3x"></i> <h4>User Menu</h4></a>


                            <%

                            %>



                            <div class="container">
                                <h1>Edit Profile</h1>
                                <hr>
                                <div class="row">
                                    <!-- left column 
                                    <div class="col-md-3">
                                        <div class="text-center">
                                            <img src="//placehold.it/100" class="avatar img-circle" alt="avatar">
                                            <h6>Upload a different photo...</h6>
            
                                            <input type="file" class="form-control">
                                        </div>
                                    </div>
                                    -->
                                    <!-- edit form column -->
                                    <div class="col-md-9 personal-info">
                                        <div class="alert alert-info alert-dismissable">
                                            <a class="panel-close close" data-dismiss="alert">×</a> 
                                            Edit your page and then click "Save Changes" button
                                        </div>
                                        <h3>Personal info</h3>

                                        <form class="form-horizontal" role="form" name="myForm" action="http://localhost:8080/Dilean/editUserAction.jsp?id=1" onsubmit="return(validate());" >
                                            <div class="form-group">
                                                <label class="col-md-3 control-label">First name:</label>
                                                <div class="col-md-8">
                                                    <input class="form-control" type="text" value="" name="first_name" placeholder="<%=firstName.getValue()%>">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="col-md-3 control-label">Last name:</label>
                                                <div class="col-md-8">
                                                    <input class="form-control" type="text" value="" name="last_name" placeholder="<%=lastName.getValue()%>">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="col-md-3 control-label">Status:</label>
                                                <div class="col-md-8">
                                                    <input class="form-control" type="text" value="" name="status" placeholder="<%=status.getValue()%>">
                                                </div>
                                            </div>


                                            <div class="form-group">
                                                <label class="col-md-3 control-label">Email:</label>
                                                <div class="col-md-8">
                                                    <table  id="tab_logic">
                                                        <tbody>

                                                            <%

                                                                for (int i = 0; i < emails.getValue().size(); i++) {


                                                            %>
                                                            <tr id='addr0'>
                                                        <input type="text" name='mail<%=i%>' placeholder='<%=emails.getValue().get(i)%>' class="form-control"/>
                                                        </tr>

                                                        <%
                                                            }
                                                        %>

                                                        <tr id='addr1'></tr>

                                                        </tbody>
                                                    </table>
                                                    <a id="add_row" class="btn btn-default pull-left">Add Row</a><a id='delete_row' class="pull-right btn btn-default">Delete Row</a>
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <label class="col-md-3 control-label">Phone Number:</label>
                                                <div class="col-md-8">
                                                    <table  id="tab_logic1">
                                                        <tbody>
                                                            <%
                                                                for (int i = 0; i < telephone.getValue().size(); i++) {


                                                            %>


                                                            <tr id='tpn0'>
                                                        <input type="text" name='phoneNumber0' placeholder='<%=telephone.getValue().get(i)%>' class="form-control"/>
                                                        </tr>

                                                        <%
                                                            }
                                                        %>

                                                        <tr id='tpn1'>

                                                        </tr>
                                                        </tbody>
                                                    </table>
                                                    <a id="add_row1" class="btn btn-default pull-left">Add Row</a><a id='delete_row1' class="pull-right btn btn-default">Delete Row</a>
                                                </div>
                                            </div>






                                            <div class="form-group">
                                                <label class="col-md-3 control-label">Birth Day:</label>
                                                <div class="col-md-2">


                                                    <select name="day" class = "form-control input-sm">
                                                        <%
                                                            for (int day = 1; day <= 31; day++) {
                                                        %>

                                                        <option value="<%=day%>"
                                                                <%
                                                                    if (birthDate.getValue().getDay() == day) {

                                                                %>
                                                                selected="selected"
                                                                <%                                                                            }

                                                                %>

                                                                ><%=day%></option>


                                                        <%
                                                            }


                                                        %>
                                                    </select>   

                                                    <select name="month" class = "form-control input-sm">

                                                        <%                                                            for (int month = 1; month <= 12; month++) {
                                                        %>

                                                        <option value="<%=month%>"
                                                                <%
                                                                    if (birthDate.getValue().getMonth() == month) {

                                                                %>
                                                                selected="selected"
                                                                <%                                                                            }

                                                                %>

                                                                ><%=month%></option>


                                                        <%
                                                            }


                                                        %>



                                                    </select> 

                                                    <select name="year" class = "form-control input-sm">
                                                        <%                                                             for (int year = 1920; year <= 2014; year++) {
                                                        %>

                                                        <option value="<%=year%>"
                                                                <%
                                                                    if (birthDate.getValue().getYear() == (year % 100)) {

                                                                %>
                                                                selected="selected"
                                                                <%                                                                            }

                                                                %>

                                                                ><%=year%></option>


                                                        <%
                                                            }


                                                        %>
                                                    </select> 

                                                </div>
                                            </div>




                                            <div class="form-group">
                                                <label class="col-md-3 control-label">Gender:</label>
                                                <div class="col-md-8">
                                                    <div class="ui-select">
                                                        <select id="user_time_zone" class="form-control" name = "gender">

                                                            <%                                                                int num = 1;
                                                                for (AttributeListValue alv : genList) {

                                                            %>
                                                            <option value="<%=alv.getName()%>"
                                                                    <%
                                                                    if(gender.getValue().getName().compareTo(alv.getName())==0)
                                                                    {
                                                                        %>
                                                                        selected="selected"
                                                                        <%
                                                                    }
                                                                    
                                                                    %>
                                                                    
                                                                    ><%=alv.getName()%></option>

                                                            <%              num++;
                                                                }

                                                            %>



                                                        </select>
                                                    </div>
                                                </div>
                                            </div>


                                            <div class="form-group">
                                                <label class="col-md-3 control-label">Relationship Status</label>
                                                <div class="col-md-8">
                                                    <div class="ui-select">
                                                        <select id="user_time_zone" class="form-control" name="RSStatus">
                                                            <%                                                                
                                                                for (AttributeListValue alv : relList) {

                                                            %>
                                                            <option value="<%=alv.getName()%>"
                                                                    <%
                                                                    if(relStatus.getValue().getName().compareTo(alv.getName())==0){
                                                                        %>
                                                                        selected="selected"
                                                                        <%
                                                                    }
                                                                    
                                                                    %>
                                                                    
                                                                    
                                                                    ><%=alv.getName()%></option>

                                                            <%              num++;
                                                                }

                                                            %>
                                                        </select>
                                                    </div>
                                                </div>
                                            </div>





                                            <div class="form-group">
                                                <label class="col-md-3 control-label">Languadges:</label>
                                                <div class="col-md-8">

                                                    <%                                                         num = 1;
                                                        for (AttributeListValue alv : list) {

                                                    %>
                                                    <label><input type="checkbox" name="lang<%=num%>"
                                                                  
                                                                  <%
                                                                   for (int i = 0; i < languages.getValue().size(); i++) {
                                                                       if(languages.getValue().get(i).getName().compareTo(alv.getName())==0){
                                                                           %>
                                                                           checked="checked"
                                                                           <%
                                                                       }
                                                                   }
                                                                  %>
                                                                  
                                                                  
                                                                  ><%=alv.getName()%> </label>
                                                        <%              num++;
                                                            }

                                                        %>



                                                </div>

                                            </div>





                                            <div class="form-group">
                                                <label class="col-md-3 control-label">Univercity</label>
                                                <div class="col-md-8">
                                                    <div class="ui-select">
                                                        <select id="user_time_zone" class="form-control" name="univercity">
                                                             <%                                                        
                                                        for (AttributeListValue alv : unList) {

                                                    %>
                                                    <option value="<%=alv.getName()%>"
                                                            <%
                                                            if(univercity.getValue().getName().compareTo(alv.getName())==0){
                                                                %>
                                                                selected="selected"
                                                                <%
                                                            }
                                                            
                                                            %>
                                                            
                                                            
                                                            
                                                            ><%=alv.getName()%></option>
                                                        <%              num++;
                                                            }

                                                        %>

                                                        </select>
                                                    </div>
                                                </div>
                                            </div>


                                            <div class="form-group">
                                                <label class="col-md-3 control-label">Password:</label>
                                                <div class="col-md-8">
                                                    <input class="form-control" type="password" value="" name="password">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="col-md-3 control-label">Confirm password:</label>
                                                <div class="col-md-8">
                                                    <input class="form-control" type="password" value="" name="confirm_password">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="col-md-3 control-label"></label>
                                                <div class="col-md-8">
                                                    <input type="submit" class="btn btn-primary" value="Save Changes">
                                                    <span></span>
                                                    <input type="reset" class="btn btn-default" value="Cancel">


                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>






                            <!-- /#wrapper -->

                            <!-- jQuery Version 1.11.0 -->
                            <script src="resources/mytheme/js/jquery-1.11.0.js"></script>

                            <!-- Bootstrap Core JavaScript -->
                            <script src="resources/mytheme/js/bootstrap.min.js"></script>

                            <!-- Menu Toggle Script -->
                            <script>
                                            $("#menu-toggle").click(function(e) {
                                                e.preventDefault();
                                                $("#wrapper").toggleClass("toggled");
                                            });

                                            $(document).ready(function() {
                                                var i = 1;
                                                $("#add_row").click(function() {
                                                    $('#addr' + i).html("<input  name='mail" + i + "' type='text' placeholder='Mail'  class='form-control'>\n\
                    "
                                                            );

                                                    $('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
                                                    i++;
                                                });
                                                $("#delete_row").click(function() {
                                                    if (i > 1) {
                                                        $("#addr" + (i - 1)).html('');
                                                        i--;
                                                    }
                                                });

                                            });

                                            $(document).ready(function() {
                                                var i = 1;
                                                $("#add_row1").click(function() {
                                                    $('#tpn' + i).html("<input  name='phoneNumber" + i + "' type='text' placeholder='regexp'  class='form-control'>\n\
                    "
                                                            );

                                                    $('#tab_logic1').append('<tr id="tpn' + (i + 1) + '"></tr>');
                                                    i++;
                                                });
                                                $("#delete_row1").click(function() {
                                                    if (i > 1) {
                                                        $("#tpn" + (i - 1)).html('');
                                                        i--;
                                                    }
                                                });

                                            });


                                            function validate()
                                            {



                                                if ((document.myForm.password.value) == (document.myForm.confirm_password.value))
                                                {
                                                    return true;
                                                }
                                                else {
                                                    myForm.password.value = "";
                                                    myForm.confirm_password.value = "";
                                                    alert("Password Must be equal!");
                                                    return false;
                                                }
                                                return true;
                                            }
                                            ;
                            </script>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>

</html>
